/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cinfo.siga.gestionacademica.ui.visual;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.cinfo.core.entidades.*;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.cinfo.siga.gestionacademica.datos.impl.DAO;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.cinfo.siga.gestionacademica.ui.visual//GestionarPreciosXModulo//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "GestionarPreciosXModuloTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "output", openAtStartup = false)
@ActionID(category = "Window", id = "org.cinfo.siga.gestionacademica.ui.visual.GestionarPreciosXModuloTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_GestionarPreciosXModuloAction",
        preferredID = "GestionarPreciosXModuloTopComponent")
@Messages({
   "CTL_GestionarPreciosXModuloAction=GestionarPreciosXModulo",
   "CTL_GestionarPreciosXModuloTopComponent=GestionarPreciosXModulo Window",
   "HINT_GestionarPreciosXModuloTopComponent=This is a GestionarPreciosXModulo window"
})
public final class GestionarPreciosXModuloTopComponent extends TopComponent {

   public GestionarPreciosXModuloTopComponent() {
      initComponents();
      setName(Bundle.CTL_GestionarPreciosXModuloTopComponent());
      setToolTipText(Bundle.HINT_GestionarPreciosXModuloTopComponent());
      programaComboBox.removeAllItems();
      dao.datosEjemplo();
      iniciar();
   }

   /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      txtPrograma = new javax.swing.JLabel();
      txtModulo = new javax.swing.JLabel();
      programaComboBox = new javax.swing.JComboBox();
      moduloComboBox = new javax.swing.JComboBox();
      scrollTabla = new javax.swing.JScrollPane();
      tabla = new javax.swing.JTable();
      GrabarButton = new javax.swing.JButton();
      CancelButton = new javax.swing.JButton();

      org.openide.awt.Mnemonics.setLocalizedText(txtPrograma, org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.txtPrograma.text")); // NOI18N

      org.openide.awt.Mnemonics.setLocalizedText(txtModulo, org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.txtModulo.text")); // NOI18N

      programaComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
      programaComboBox.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            programaComboBoxActionPerformed(evt);
         }
      });

      moduloComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

      tabla.setModel(new javax.swing.table.DefaultTableModel(
         new Object [][] {
            {"1", "EXTERNO", ""},
            {"2", "SAN MARCOS", ""},
            {"3", "ADMINISTRATIVO", ""},
            {"4", "DOCENTE", ""}
         },
         new String [] {
            "Nro.", "TIPO DE ALUMNO", "PRECIO"
         }
      ) {
         boolean[] canEdit = new boolean [] {
            false, false, true
         };

         public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
         }
      });
      tabla.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
      tabla.getTableHeader().setResizingAllowed(false);
      tabla.getTableHeader().setReorderingAllowed(false);
      scrollTabla.setViewportView(tabla);
      tabla.getColumnModel().getColumn(0).setResizable(false);
      tabla.getColumnModel().getColumn(0).setPreferredWidth(20);
      tabla.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.tabla.columnModel.title0")); // NOI18N
      tabla.getColumnModel().getColumn(1).setResizable(false);
      tabla.getColumnModel().getColumn(1).setPreferredWidth(300);
      tabla.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.tabla.columnModel.title1")); // NOI18N
      tabla.getColumnModel().getColumn(2).setResizable(false);
      tabla.getColumnModel().getColumn(2).setPreferredWidth(50);
      tabla.getColumnModel().getColumn(2).setHeaderValue(org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.tabla.columnModel.title2")); // NOI18N

      org.openide.awt.Mnemonics.setLocalizedText(GrabarButton, org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.GrabarButton.text")); // NOI18N
      GrabarButton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            GrabarButtonActionPerformed(evt);
         }
      });

      org.openide.awt.Mnemonics.setLocalizedText(CancelButton, org.openide.util.NbBundle.getMessage(GestionarPreciosXModuloTopComponent.class, "GestionarPreciosXModuloTopComponent.CancelButton.text")); // NOI18N
      CancelButton.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            CancelButtonActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(scrollTabla, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                     .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addComponent(txtModulo)
                           .addComponent(txtPrograma))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addComponent(programaComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                           .addComponent(moduloComboBox, 0, 317, Short.MAX_VALUE))
                        .addGap(37, 37, 37))))
               .addGroup(layout.createSequentialGroup()
                  .addGap(109, 109, 109)
                  .addComponent(GrabarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(41, 41, 41)
                  .addComponent(CancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(txtPrograma)
               .addComponent(programaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(txtModulo)
               .addComponent(moduloComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(scrollTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addComponent(CancelButton)
               .addComponent(GrabarButton))
            .addContainerGap())
      );
   }// </editor-fold>//GEN-END:initComponents

   private void programaComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_programaComboBoxActionPerformed
      try{
         Modulo mod;
         ArrayList<ProgramaModulo> pmlist;
         
         moduloComboBox.removeAllItems();
         if(programaComboBox.getItemCount() == 0) return;
         pmlist = dao.getProgramaModulosxProg(((Programa)programaComboBox.getSelectedItem()).getCodigo());
         if(pmlist == null) return;
         for(ProgramaModulo pm: pmlist){
            mod = dao.getModulo(pm.getCodModulo());
            if(mod == null) continue;
            moduloComboBox.addItem(mod);
         }
      }catch(Exception e){ JOptionPane.showMessageDialog(this, e.getMessage() + "...!"); }
   }//GEN-LAST:event_programaComboBoxActionPerformed

   private void GrabarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GrabarButtonActionPerformed
      try{
         throw new Exception("Graba");
      }catch(Exception e){ JOptionPane.showMessageDialog(this, e.getMessage() + "...!"); }
   }//GEN-LAST:event_GrabarButtonActionPerformed

   private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
      try{
         throw new Exception("Cancela");
      }catch(Exception e){ JOptionPane.showMessageDialog(this, e.getMessage() + "...!"); }
   }//GEN-LAST:event_CancelButtonActionPerformed
   
   private void iniciar() {
      try{
         JOptionPane.showMessageDialog(this, "Inicializa...");
         ArrayList<Programa> aux = dao.getProgramas();

         for(Programa p : aux) {
            programaComboBox.addItem(p);
         }
      }catch(Exception e){ JOptionPane.showMessageDialog(this, e.getMessage() + "...!"); }
   }
   private DAO dao = new DAO();
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton CancelButton;
   private javax.swing.JButton GrabarButton;
   private javax.swing.JComboBox moduloComboBox;
   private javax.swing.JComboBox programaComboBox;
   private javax.swing.JScrollPane scrollTabla;
   private javax.swing.JTable tabla;
   private javax.swing.JLabel txtModulo;
   private javax.swing.JLabel txtPrograma;
   // End of variables declaration//GEN-END:variables

   @Override
   public void componentOpened() {
      // TODO add custom code on component opening
   }

   @Override
   public void componentClosed() {
      // TODO add custom code on component closing
   }

   void writeProperties(java.util.Properties p) {
      // better to version settings since initial version as advocated at
      // http://wiki.apidesign.org/wiki/PropertyFiles
      p.setProperty("version", "1.0");
      // TODO store your settings
   }

   void readProperties(java.util.Properties p) {
      String version = p.getProperty("version");
      // TODO read your settings according to their version
   }
}
